﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using NetDon.Utilities;

namespace NetDon.OA.Interface.Admin
{
  
    [Serializable]
    public class CompanyData : DomainObject, IHierarchyData
    {
        public Guid CompanyId { get; set; }
        public string CompanyCode { get; set; }
        public string CompanyName { get; set; }
        public string CompanyDesc { get; set; }
        public string Address { get; set; }
        public string PostCode { get; set; }
        public string Telephone { get; set; }
        public string Fax { get; set; }
        public string Website { get; set; }
        public string Email { get; set; }
        public int? NodeLevel { get; set; }
        public Guid? ParentId { get; set; }
        public string CEO { get; set; }
        public string CEOAssistant { get; set; }

      

        #region Implementation of IHierarchyData

        public IHierarchicalEnumerable GetChildren()
        {
            var service = ServiceContainer.GetService<ICompanyService>();
            return new CompanyDataCollection(service.GetChildCompanies(CompanyId));
        }

        public IHierarchyData GetParent()
        {
            if (ParentId.HasValue)
            {
                var service = ServiceContainer.GetService<ICompanyService>();
                return service.GetCompany(ParentId.Value);
            }
            return null;
        }

        public bool HasChildren
        {
            get
            {
                var service = ServiceContainer.GetService<ICompanyService>();
                return service.GetChildCompanies(CompanyId).Count > 0 || ServiceContainer.GetService<IDepartmentService>().GetRootDepartments(CompanyId).Count>0;
            }
        }

        public string Path
        {
            get;
            set;
        }

        public object Item
        {
            get { return this; }
        }

        public string Type
        {
            get { return typeof(CompanyData).FullName; }
        }

        #endregion
    }

    public class CompanyDataCollection:Collection<CompanyData>, IHierarchicalEnumerable
    {
        public CompanyDataCollection()
        {
        }

        public CompanyDataCollection(IEnumerable<CompanyData> categories)
        {
            foreach (var category in categories)
            {
                Add(category);
            }
        }
        #region Implementation of IHierarchicalEnumerable

        public IHierarchyData GetHierarchyData(object enumeratedItem)
        {
            return (CompanyData)enumeratedItem;
        }

        #endregion
    }
}
